<template>
	<full-screen-dialog :title="'调拨审核'" v-model="visible" :width="1200" destroy-on-close :close-on-click-modal="false">
		<el-form :model="entityObj" :rules="rules" disabled ref="dialogForm" label-width="100px">
			<el-row>
				<el-col :span="8">
					<el-form-item label-width="120px" label="调拨时间" prop="allocateDate">
						<el-date-picker v-model="entityObj.allocateDate" type="datetime" value-format="YYYY-MM-DD"
							format="YYYY-MM-DD" placeholder="请输入调拨时间" style="width: 100%" />
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label-width="120px" label="调出仓库" prop="OutWarehouseId">
						<sc-warehouse-select ref="outorgselect" :props="warehouseProps"
							:selectKeyValue="'OutWarehouseId'" v-model="entityObj.outwarehouses"
							@fatherMethod="outwarehouseselect" :multiple="false"
							style="width: 100%"></sc-warehouse-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label-width="120px" label="调入仓库" prop="InWarehouseId">
						<sc-warehouse-select ref="inorgselect" :props="warehouseProps" :selectKeyValue="'InWarehouseId'"
							v-model="entityObj.inwarehouses" @fatherMethod="inwarehouseselect" :multiple="false"
							style="width: 100%"></sc-warehouse-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label-width="120px" label="备注">
				<el-input v-model="entityObj.remarks" style="width: 100%;" type="textarea" :rows="3" />
			</el-form-item>
			<el-row>
				<el-col :span="24">
					<el-form-item label-width="120px">
						<el-form inline label-width="49px" style="margin-top: 5px">
							<el-form-item label="物料编号" label-width="80px">
								<el-input placeholder="---请输入编号---" v-model="queryList.materialCode"
									style="width: 160px" />
							</el-form-item>
							<el-form-item label="物料名称" label-width="80px">
								<el-input placeholder="---请输入名称---" v-model="queryList.materialName"
									style="width: 160px" />
							</el-form-item>
							<el-button :icon="Search"   type="primary"
								@click="handleSearch">查询</el-button>
							<el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
							<el-form inline label-width="49px" style="margin-top: 5px"></el-form>
						</el-form>
						<el-table :data="itemsData" border style="width: 100%" show-summary>
							<el-table-column label="物料编码" width="100">
								<template #default="scope">{{scope.row.materialCode}}</template>
							</el-table-column>
							<el-table-column label="物料名称" width="130">
								<template #default="scope">{{scope.row.materialName}}</template>
							</el-table-column>
							<el-table-column label="规格" width="120">
								<template #default="scope">{{scope.row.materialSpecs}}</template>
							</el-table-column>
							<el-table-column label="物料类别" width="120">
								<template #default="scope">{{scope.row.materialTypeName}}</template>
							</el-table-column>
							<el-table-column label="单位" width="80">
								<template #default="scope">{{scope.row.unitName}}</template>
							</el-table-column>
							<el-table-column label="数量" prop="qty">
								<template #default="scope">
									<el-input-number v-model="scope.row.qty" style="width: 100%" :step="1"
										controls-position="right"></el-input-number>
								</template>
							</el-table-column>
							<el-table-column label="成本价" prop="price">
								<template #default="scope">
									<el-input-number v-model="scope.row.price" style="width: 100%" :step="0.01"
										controls-position="right"></el-input-number>
								</template>
							</el-table-column>
							<el-table-column label="金额" width="90" prop="totalAmount">
								<template
									#default="scope">{{scope.row.totalAmount=scope.row.qty*scope.row.price}}</template>
							</el-table-column>
							<el-table-column label="备注">
								<template #default="scope">
									<el-input v-model="scope.row.remarks" style="width: 100%" />
								</template>
							</el-table-column>
							<el-table-column label="操作" width="80">
								<template #default="scope">
									<el-button size="small" @click="deleteRow(scope.$index, scope.row)">删除</el-button>
								</template>
							</el-table-column>
						</el-table>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label-width="120px" label="审核意见">
				<el-input v-model="entityObj.ReviewedRemarks" style="width: 100%;" type="textarea" :rows="3" />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="reviewed(1)">审核通过</el-button>
			<el-button type="primary" :loading="isSaveing" @click="reviewed(2)">审核不通过</el-button>
		</template>
	</full-screen-dialog>
</template>

<script>
	import tool from "@/utils/tool";
	export default {
		data() {
			return {
				materialState: false,
				warehouseProps: {
					label: "warehouseName",
					value: "warehouseId",
				},
				queryList: {},
				mode: "Add",
				visible: false,
				entityObj: {
					allocateOrderItems: []
				},
				itemsData: [],
				defaultTime:[
					new Date(2000, 1, 1, 0, 0, 0),
					new Date(2000, 2, 1, 23, 59, 59),
				],
				rules: {}
			}
		},
		methods: {
			open() {
				this.visible = true;
				this.entityObj = {
					allocateOrderItems: []
				};
				this.itemsData = [];
			},
			async setData(data) {
				this.entityObj = Object.assign(this.entityObj, data);
				this.entityObj.outwarehouses = {
					warehouseId: this.entityObj.outWarehouseId,
					warehouseName: this.entityObj.outWarehouseName
				}
				this.entityObj.inwarehouses = {
					warehouseId: this.entityObj.inWarehouseId,
					warehouseName: this.entityObj.inWarehouseName
				}
				try {
					var returnValue = await this.$API.auth.PostProcessApi.post(
						"allocate-order/items", {
							AllocateOrderId: this.entityObj.allocateOrderId
						}
					);
					if (returnValue.status == 200) {
						this.entityObj.allocateOrderItems = returnValue.response;
						this.handleSearch();
					} else {
						this.$message.error("操作失败:" + returnValue.msg);
					}
				} catch (error) {
					this.loading = false;
					return false;
				}

			},
			handleSearch() {
				this.itemsData = this.entityObj.allocateOrderItems;
				console.log(this.entityObj.allocateOrderItems)
				if (this.queryList.materialCode) {
					this.itemsData = this.itemsData.filter((item) => {
						return (
							item.materialCode.includes(
								this.queryList.materialCode
							)
						);
					});
				}

				if (this.queryList.materialName) {
					this.itemsData = this.itemsData.filter((item) => {
						return item.materialName.includes(
							this.queryList.materialName
						);
					});
				}
			},
			deleteRow(index) {
				this.entityObj.allocateOrderItems.splice(index, 1);
				this.handleSearch();
			},
			outwarehouseselect(val) {
				console.log(val);
			},
			setMaterialChoice() {
				let materials = this.$refs.scMaterialChoice.selectionRows;
				materials.map((p) => {
					debugger;
					this.entityObj.allocateOrderItems.push({
						materialId: p.materialId,
						materialCode: p.materialCode,
						materialName: p.materialName,
						materialSpecs: p.materialSpecs,
						materialTypeId: p.materialTypeId,
						materialTypeName: p.materialTypeName,
						unitName: p.unitName,
						qty: 0,
						price: 0,
						totalAmount: 0,
						remarks: "",
						materialItemId: p.materialItemId,
					});
				});
				this.materialState = false;
				this.handleSearch();
			},
			handleBeforeClose() {
				this.$confirm("信息未保存,确定要关闭吗？", "提示", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning",
					})
					.then(() => {
						this.visible = false;
					})
					.catch(() => {
						// 用户点击了取消按钮，不执行关闭
					});
			},
			//表单提交方法
			reviewed(status) {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						try {
							this.entityObj.ReviewedStatus=status;
							var returnValue =
								await this.$API.auth.PostProcessApi.post('allocate-order/reviewedAllocateOrder',
									this.entityObj
								);
							if (returnValue.status == 200) {
								this.$message.success("操作成功");
								this.$emit("fatherMethod");
								this.isSaveing = false;
								this.visible = false;
							} else {
								this.$message.error("操作失败:" + returnValue.msg);
								this.isSaveing = false;
							}
						} catch (error) {
							this.loading = false;
							this.isSaveing = false;
							return false;
						}
					}
				});
			},

		}
	}
</script>

<style>
</style>
